import sys, os
sys.path.append('src/')
import numpy as np
import pylab as pl
import scipy.fftpack as spf
import scipy.signal as sps
import numpy.fft as npf

import transceiver
import misc

fftlen = 1024
Fs = fftlen/(1./15000)

RB = 50
CP = True
modsym = 4

bb = transceiver.Baseband(RB,fftlen)
ps = misc.PulseShaping(Fs)
cc = misc.Calc()
dh = misc.DataHandling()

x16qam = np.random.randint(0,2,48*RB*modsym)
xmod,xtemp = bb.sc_fdm(x16qam,modsym,CP,'16QAM',modout=True,shift=True)


# Filtering:
fco = 5E6

xps = ps.rect(xmod,fco)

# RF:
fc = 700E6
rf = transceiver.RFFE(fc,fftlen)

RFout = rf.tx(xps)

RFin = RFout

yadc = rf.rx(RFin,fco)

# Demodulation 

ydem = bb.sc_fdm_dem(yadc,modsym,CP)

# EVM:

EVM = cc.EVM(ydem,xtemp)

print EVM

dh.write_data(np.real(ydem),np.imag(ydem),'tex/data/iq.txt')

""" Reference IQ """
x2 = np.array([0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,0,1,0,1,0,1,1,0,0,1,1,1,1,0,0,0,1,0,0,1,1,0,1,0,1,0,1,1,1,1,0,0,1,1,0,1,1,1,1,0,1,1,1,1])
test = bb.modulator(x2,'16QAM',True)

dh.write_data(np.real(test),np.imag(test),'tex/data/iq_comp.txt')
